<template>
  <d2-container>

    <template slot="header">

      <el-form :gutter="24" ref="search-box" :model="mainQuery" label-position="left" :inline="true">
        <el-form-item :label="$t('page-sys-role.search-box.name')" size="mini">
          <el-input placeholder="" v-model="mainQuery.name" @keyup.enter.native="fetchMainData" clearable></el-input>
        </el-form-item>
        <el-form-item :label="$t('page-sys-role.search-box.type')" size="mini">
          <el-select v-model="mainQuery.type" :placeholder="$t('page.form.all')" clearable @change="fetchMainData">
            <el-option v-for="item in boolOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('page-sys-role.search-box.status')" size="mini">
          <el-select v-model="mainQuery.status" :placeholder="$t('page.form.all')" clearable @change="fetchMainData">
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div class="search-btn-box">
        <div class="left">
          <el-button-group>
            <el-button class="filter-item" icon="el-icon-plus" type="success" @click="handleAdd" :loading="mainTable.loading" size="mini">{{ $t('page.search-box.add') }}</el-button>
          </el-button-group>
        </div>
        <div class="right">
          <el-button class="filter-item" icon="el-icon-refresh" type="default" @click="resetMainData" :loading="mainTable.loading" size="mini">{{ $t('page.search-box.reset') }}</el-button>
          <el-button class="filter-item" icon="el-icon-search" type="primary" @click="fetchMainData" :loading="mainTable.loading" size="mini">{{ $t('page.search-box.search') }}</el-button>
        </div>
      </div>
    </template>

    <!--主列表数据-->
    <d2-crud-x ref="d2Crud" :options="mainTable.options" :index-row="mainTable.indexRow" :columns="mainTable.columns" :data="mainTable.list" :selection-row="mainTable.allowSelect" :loading="mainTable.loading" :row-handle="mainTable.rowHandle" @current-change="onMainCurrentChange" @row-dblclick="onMainDoubleClick" @selection-change="" @handle-node-set="handleNodeSet" @handle-set="handleSet"></d2-crud-x>

    <template slot="footer">
      <el-pagination @size-change="handleMainSizeChange" @current-change="handleMainPageChange" :current-page="mainTable.pagination.currentPage" :page-sizes="mainTable.pagination.pageSizes" :page-size="mainTable.pagination.pageSize" :page-count="mainTable.pagination.pageCount" :total="mainTable.pagination.total" layout="total, sizes, prev, pager, next, jumper" :background="true">
      </el-pagination>
    </template>

    <dialog-add ref="dialog-add" @dialog-close="fetchMainData" :statusOptions="statusOptions" :boolOptions="boolOptions"></dialog-add>

    <dialog-node-set ref="dialog-node-set" @dialog-close="fetchMainData"></dialog-node-set>

  </d2-container>
</template>

<script>

import util from '@/libs/util';
import ApiRole from '@/api/system.role';
import DialogAdd from './dialog-add';
import DialogNodeSet from './dialog-node-set';
import i18nTable from "@/mixins/i18n-table";
export default {
  mixins: [i18nTable
  ],
  components: {
    DialogAdd,
    DialogNodeSet,
  },
  data() {
    return {

      statusOptions: [],
      boolOptions: [],

      mainQueryRaw: null,
      mainQuery: {
        name: '',
        type: '',
        status: '',
      },

      mainTable: {
        loading: false,
        options: {
          border: true,
          stripe: true,
          highlightCurrentRow: true,
          emptyText: this.$t('page-sys-role.d2-crud.empty-data'),
          size: 'mini',
        },
        // indexRow: {
        //   title:this.$t('page.form.serialnumber'),
        //   width: "80",
        //   align:'center'
        // },
        indexRow: null,
        allowSelect: false,
        columns: [
          { title: "角色名称", t: "page-sys-role.d2-crud.column-name", key: 'name', align: 'center', showOverflowTooltip: true },
          { title: "备注", t: "page-sys-role.d2-crud.column-remark", key: 'remark', align: 'center', showOverflowTooltip: true },
          { title: "是否超级角色", t: "page-sys-role.d2-crud.column-type", key: 'strType', align: 'center', showOverflowTooltip: true },
          { title: "状态", t: "page-sys-role.d2-crud.column-status", key: 'strStatus', align: 'center', showOverflowTooltip: true },
          { title: "创建时间", t: "page-sys-role.d2-crud.column-createTime", key: 'createTime', align: 'center', showOverflowTooltip: true },
        ],
        rowHandle: {
          width: '260',
          align: 'center',
          fixed: 'right',
          t: "page.form.operation",
          columnHeader: this.$t('page.form.operation'),
          custom: [
            {
              text: "修改",
              t: "page.search-box.edit",
              icon: 'el-icon-edit',
              type: 'default',
              size: 'mini',
              emit: 'handle-set'
            },
            {
              text: '节点权限',
              t: "page.search-box.nodePermissions",
              icon: '',
              type: 'default',
              size: 'mini',
              emit: 'handle-node-set'
            },
          ]
        },
        list: [],
        pagination: {
          currentPage: 1,
          pageSize: 50,
          total: 0,
          pageSizes: [50, 100, 200]
        },
        current: null,
      },
    }
  },
  mounted() {
    this.mainQueryRaw = Object.copy(this.mainQuery);
    this.fetchOptions();
    this.resetMainData();
  },
  beforeDestroy() {
  },
  methods: {

    renderMainData(re) {

      // re.list.forEach(item => {
        // item.strType = util.tool.strFromOptions(item.type, this.boolOptions, '-');
        // item.strStatus = util.tool.strFromOptions(item.status, this.statusOptions, '-');
        // item.createTime = dayjs(item.createTime * 1e3).format('YYYY-MM-DD HH:mm:ss')
      // });

      console.log(Object.copy(re.list));

      this.mainTable.list = re.list;
      this.mainTable.pagination.total = re.total;
    },

    handleMainSizeChange(val) {
      this.mainTable.pagination.pageSize = val;
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData()
    },
    handleMainPageChange(val) {
      this.mainTable.pagination.currentPage = val;
      this.fetchMainData()
    },

    buildMainQuery() {
      var query = {
        page: this.mainTable.pagination.currentPage,
        limit: this.mainTable.pagination.pageSize,
        search: this.mainQuery,
      };

      return query;
    },

    // 重置表单
    resetMainData() {
      this.mainQuery = Object.copy(this.mainQueryRaw);
      this.fetchMainData();
    },

    async fetchMainData() {

      this.mainTable.loading = true;

      try {
        var re = await ApiRole.list(this.buildMainQuery());
        this.renderMainData(re);
      } catch (err) {
        console.error(err);
      }

      this.mainTable.loading = false;
    },

    async fetchOptions() {
      var re = await ApiRole.statusOptions();
      this.statusOptions = re;

      var re = await ApiRole.isSuperOptions();
      this.boolOptions = re;
    },

    onMainCurrentChange(now, old) {
      this.mainTable.current = now;
    },

    onMainDoubleClick(now) {
      this.mainTable.current = now;
      // this.handleSet();
    },

    handleAdd() {
      this.$refs['dialog-add'].show();
    },

    handleSet({ index, row }) {
      this.$refs['dialog-add'].show(row.id);
    },

    handleNodeSet({ index, row }) {
      this.$refs['dialog-node-set'].show(Object.copy(row));
    },
  }
}
</script>

<style>
</style>
